package own.stu.jobgib.playown.fsm.hfsm;

import lombok.extern.slf4j.Slf4j;
import own.stu.jobgib.playown.fsm.hfsm.states.StateBasketball;
import own.stu.jobgib.playown.fsm.hfsm.states.StateEat;
import own.stu.jobgib.playown.fsm.hfsm.states.StateHomeWork;
import own.stu.jobgib.playown.fsm.hfsm.states.StateReset;

import java.util.HashMap;
import java.util.Map;

@Slf4j
public class StateMachine {
    // 保存所有的状态
    private Map<StateEnum, StateBase> stateDic = new HashMap<>();

    // 记录当前状态
    private StateBase currentState;

    public StateMachine() {
        // 初始化状态、并存储

        stateDic.put(StateEnum.EAT, new StateEat());
        stateDic.put(StateEnum.BASKETBALL, new StateBasketball());
        stateDic.put(StateEnum.HOMEWORK, new StateHomeWork());
        stateDic.put(StateEnum.RESET, new StateReset());
    }

    // 状态转换方法
    public void transitionState(StateEnum stateEnum) {
        // 如果当前状态不为空，先退出当前状态
        if (currentState != null) {
            currentState.onExit();
        }

        // 令当前状态等于转换的新状态
        currentState = stateDic.get(stateEnum);
        // 转换的新状态执行 进入方法
        currentState.onEnter();
    }

    // 每帧执行的方法
    public void OnExecute() {
        if (null != currentState) {
            currentState.onExecute();
        }
    }

    public static void main(String[] args) {
        log.info("wefwfwfwfe{},{},{}", "ewfwef", "wefwefw", "ewfwfwf");
    }
}
